#!/usr/bin/env python

"""A fork that demonstrates a copied environment"""

# Reference: http://www.ibm.com/developerworks/cn/aix/library/au-multiprocessing/

import os
from os import environ
import time

def my_fork():
    environ['FOO'] = "abc"
    print "FOO environmental variable set to: %s" % environ['FOO']
    environ['FOO'] = "abd"
    print "FOO environmental variable changed to: %s" % environ['FOO']
    
    try:
    	child_pid = os.fork()
    except OSError, e:
    	print e
        
    if child_pid == 0:
        time.sleep(2)
        print "---- Child Process -------"
        print "Child Process: PID# %s" % os.getpid()
        print "Child FOO environmental variable == %s" % environ['FOO']
    else:
        os.wait()
        environ["FOO"] = 'abe'
        print "Parent Process: PID# %s" % os.getpid()
        print "Parent FOO environmental variable == %s" % environ['FOO']

if __name__ == "__main__":
    my_fork()
